package JavaSE.Class12.Demo3;

class Money implements Cloneable{
    public double money=9.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Person implements Cloneable{
    public String name;
    public int age;

    public Money m=new Money();

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }


    //浅克隆
//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }


    //深克隆
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person temp = (Person) super.clone();
        temp.m=(Money)m.clone();
        return temp;
        //先通过super.clone克隆一份Person给temp
        //再通过clone克隆一份Money给temp.m（相当于给Person了一张新的存折）
    }
}
